<%@page import="java.io.File"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Crop successfully</title>
</head>
<body>
<%!
static int IMG_COUNT = 0;
%>
<%
	final String IMG_SRC = "D:\\crop\\image_crop.jpg";
	final String IMG_CROPED = "D:\\crop\\image_croped" + IMG_COUNT + ".jpg";
	final String HTML_FAILED = "<p>Found some problem during corp file.</p>";
	String x = request.getParameter("x");
	String y = request.getParameter("y");
	String w = request.getParameter("w");
	String h = request.getParameter("h");
	
	System.out.println(String.format("parameter={x:%s,y:%s,w:%s,h:%s}", x, y, w, h));
	
	if(!("".equals(x) || "".equals(y)  || "".equals(w)  || "".equals(w))){
		int x1 = -1;
		int y1 = -1;
		int w1 = -1;
		int h1 = -1;
		try{
			x1 = Integer.parseInt(x);
			y1 = Integer.parseInt(y);
			w1 = Integer.parseInt(w);
			h1 = Integer.parseInt(h);
			if(x1>-1 && y1>-1 && w1>-1 && h1>-1) {
				BufferedImage im = ImageIO.read(new File(IMG_SRC));
				BufferedImage bm = im.getSubimage(x1, y1, w1, h1);
				ImageIO.write(bm, "jpg", new File(IMG_CROPED));	
				IMG_COUNT++;
			}
			
			out.println("Done!!");
		}catch(Exception ex){
			ex.printStackTrace();
			out.println(HTML_FAILED);
		}
	} else {
		out.println(HTML_FAILED);
	}
%>
</body>
</html>